package com.copyurl.util;

import com.copyurl.common.SpringAnnotations;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiModifierList;

import java.util.Objects;
import java.util.stream.Stream;

public class SpringUtil {
    public SpringUtil() {
    }

    public static boolean containsSpringAnnotation(SpringAnnotations springAnnotation, PsiModifierList modifierList) {
        if (modifierList != null) {
            PsiAnnotation[] annotations = modifierList.getAnnotations();
            return Stream.of(annotations).map(PsiAnnotation::getQualifiedName).filter(Objects::nonNull).anyMatch((name)
                    -> name.equalsIgnoreCase(springAnnotation.getQualifiedName()));
        } else {
            return false;
        }
    }

    public static boolean isRestMethod(PsiModifierList modifierList) {
        if (modifierList == null) {
            return false;
        }
        return containsSpringAnnotation(SpringAnnotations.REQUEST_MAPPING_QUALIFIED_NAME, modifierList)
                || containsSpringAnnotation(SpringAnnotations.GET_MAPPING_QUALIFIED_NAME, modifierList)
                || containsSpringAnnotation(SpringAnnotations.POST_MAPPING_QUALIFIED_NAME, modifierList)
                || containsSpringAnnotation(SpringAnnotations.PUT_MAPPING_QUALIFIED_NAME, modifierList)
                || containsSpringAnnotation(SpringAnnotations.DELETE_MAPPING_QUALIFIED_NAME, modifierList)
                || containsSpringAnnotation(SpringAnnotations.PATCH_MAPPING_QUALIFIED_NAME, modifierList);
    }
}
